{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Rearrange Words in a Sentence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: arrangeWords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #重新排列句子中的单词"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>「句子」是一个用空格分隔单词的字符串。给你一个满足下述格式的句子 <code>text</code> :</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>句子的首字母大写</li>\n",
    "\t<li><code>text</code> 中的每个单词都用单个空格分隔。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你重新排列 <code>text</code> 中的单词，使所有单词按其长度的升序排列。如果两个单词的长度相同，则保留其在原句子中的相对顺序。</p>\n",
    "\n",
    "<p>请同样按上述格式返回新的句子。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>text = &quot;Leetcode is cool&quot;\n",
    "<strong>输出：</strong>&quot;Is cool leetcode&quot;\n",
    "<strong>解释：</strong>句子中共有 3 个单词，长度为 8 的 &quot;Leetcode&quot; ，长度为 2 的 &quot;is&quot; 以及长度为 4 的 &quot;cool&quot; 。\n",
    "输出需要按单词的长度升序排列，新句子中的第一个单词首字母需要大写。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>text = &quot;Keep calm and code on&quot;\n",
    "<strong>输出：</strong>&quot;On and keep calm code&quot;\n",
    "<strong>解释：</strong>输出的排序情况如下：\n",
    "&quot;On&quot; 2 个字母。\n",
    "&quot;and&quot; 3 个字母。\n",
    "&quot;keep&quot; 4 个字母，因为存在长度相同的其他单词，所以它们之间需要保留在原句子中的相对顺序。\n",
    "&quot;calm&quot; 4 个字母。\n",
    "&quot;code&quot; 4 个字母。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>text = &quot;To be or not to be&quot;\n",
    "<strong>输出：</strong>&quot;To be or to be not&quot;\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>text</code> 以大写字母开头，然后包含若干小写字母以及单词间的单个空格。</li>\n",
    "\t<li><code>1 &lt;= text.length &lt;= 10^5</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [rearrange-words-in-a-sentence](https://leetcode.cn/problems/rearrange-words-in-a-sentence/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [rearrange-words-in-a-sentence](https://leetcode.cn/problems/rearrange-words-in-a-sentence/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"Leetcode is cool\"', '\"Keep calm and code on\"', '\"To be or not to be\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        words  = text.split(' ')\n",
    "        words[0] = words[0][0].lower()+words[0][1:]\n",
    "        words.sort(key=lambda x:len(x))\n",
    "        words[0] = words[0][0].upper()+words[0][1:]\n",
    "        new_text = ' '.join(words)\n",
    "        return new_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        lst = text.split()\n",
    "        lst[0]=lst[0].lower()\n",
    "        lst.sort(key = lambda x : len(x))\n",
    "        lst[0] = lst[0].capitalize()\n",
    "\n",
    "        return ' '.join(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        l = text.split()\n",
    "        l[0] = l[0].lower()\n",
    "        l.sort(key=lambda w:len(w))\n",
    "        l[0] = l[0].capitalize()\n",
    "        return ' '.join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        words = text.split(' ')\n",
    "        words[0] = words[0].lower()\n",
    "        words.sort(key = lambda x:len(x))\n",
    "        words[0] = words[0].capitalize()\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        words = text.split(' ')\n",
    "        words[0] = words[0].lower()\n",
    "        words.sort(key = lambda x:len(x))\n",
    "        words[0] = words[0].capitalize()\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text=text.lower()\n",
    "        text=text.replace(' ','//')\n",
    "        text=text.split('//')\n",
    "        text.sort(key=lambda x:len(x))\n",
    "        text=' '.join(text)\n",
    "        first=text[0].upper()\n",
    "        text=first+text[1:]\n",
    "        return text\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        fh = []\n",
    "        x = 0\n",
    "        for i, j in enumerate(text):\n",
    "            if j == \" \":\n",
    "                fh.append(text[x:i].lower())\n",
    "                x = i+1\n",
    "        fh.append(text[x:i+1].lower())\n",
    "        fh.sort(key=lambda x:len(x))\n",
    "        fh[0] = fh[0].title()\n",
    "        return \" \".join(fh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        words = text.lower().split()\n",
    "        newlist = sorted(words,key=lambda x : len(x))\n",
    "        ans = \" \".join(newlist)\n",
    "        ans = chr(ord(ans[0]) - ord('a') + ord('A'))+ans[1:]\n",
    "        return ans \n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.split()\n",
    "        text[0] = chr(ord(text[0][0])-ord(\"A\")+ord(\"a\")) + text[0][1:]\n",
    "        text.sort(key=lambda x:len(x))\n",
    "        text[0] = chr(ord(text[0][0])-ord(\"a\")+ord(\"A\")) + text[0][1:]\n",
    "        return \" \".join(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        ans = text.lower().split(' ')\n",
    "\n",
    "        ans.sort(key=lambda x : len(x))\n",
    "        ans[0] = ans[0].capitalize()\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        # text_lst = text.split(\" \")\n",
    "        # dic = defaultdict(list)\n",
    "        # for each in text_lst:\n",
    "        #     length = len(each)\n",
    "        #     dic[length].append(each)\n",
    "        \n",
    "        # res = \"\"\n",
    "        # for k,v in dic.values():\n",
    "        #     res += (v+\" \")\n",
    "        \n",
    "        # return res.rstrip()\n",
    "        lst = text.lower().split(\" \")\n",
    "        lst.sort(key=lambda x: len(x))\n",
    "        lst[0] = lst[0][0].upper() + lst[0][1:]\n",
    "        return \" \".join(lst)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        return \" \".join(sorted(text.lower().split(), key=lambda x: len(x))).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.split()\n",
    "        text.sort(key = lambda x:len(x))\n",
    "        text = ' '.join(text).lower()\n",
    "        text = text[0].upper()+text[1:]\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        lst = text.split()\n",
    "        lst.sort(key = lambda x : len(x))\n",
    "        return ' '.join(lst).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        arr = text.lower().split(' ')\n",
    "        arr = sorted(arr, key=len)\n",
    "        return ' '.join(arr).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower().split()\n",
    "        text.sort(key = len)\n",
    "        return ' '.join(text).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        l = text.split()\n",
    "        l[0] = l[0].lower()\n",
    "        l.sort(key=lambda w:len(w))\n",
    "        return ' '.join(l).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower().split()\n",
    "        text.sort(key=lambda x: len(x))\n",
    "        return ' '.join(text).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        lst = text.split(\" \")\n",
    "        lst.sort(key=lambda x: len(x))\n",
    "        res = \"\"\n",
    "        for i, ch in enumerate(lst):\n",
    "            if i == 0:\n",
    "                res += ch.title()\n",
    "            else:\n",
    "                res += ch.lower()\n",
    "            if i != len(lst) - 1:\n",
    "                res += \" \"\n",
    "        return res\n",
    "             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower() # 首字母转小写\n",
    "        text = text.split(\" \") # 通过 空格 分割成数组\n",
    "        text.sort(key=lambda x: len(x)) # 使用 lambda 按照元素长度排序\n",
    "        return ' '.join(text).capitalize() # 最后首字母大写\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        return \" \".join(sorted(text.lower().split(\" \"), key=lambda x: len(x))).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def merge_sort(self,ll):\n",
    "        m=len(ll)//2\n",
    "        if len(ll)<=1:\n",
    "            return ll\n",
    "        a=self.merge_sort(ll[0:m])\n",
    "        b=self.merge_sort(ll[m:])\n",
    "        i,j=0,0\n",
    "        re=[0 for _ in range(len(ll))]\n",
    "        k=0\n",
    "        while i<len(a) and j<len(b):\n",
    "            if len(a[i])<=len(b[j]):\n",
    "                re[k]=a[i]\n",
    "                k+=1\n",
    "                i+=1\n",
    "            else :\n",
    "                re[k]=b[j]\n",
    "                k+=1\n",
    "                j+=1\n",
    "        while i<len(a):\n",
    "            re[k]=a[i]\n",
    "            k+=1\n",
    "            i+=1\n",
    "        while j<len(b):\n",
    "            re[k]=b[j]\n",
    "            k+=1\n",
    "            j+=1\n",
    "        # print(re)\n",
    "        return re\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        words=text.split()\n",
    "        words[0]=words[0].lower()\n",
    "        words=self.merge_sort(words)\n",
    "        words[0]=words[0].title()\n",
    "        return \" \".join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        words = text.lower().split(\" \")\n",
    "        words.sort(key=lambda x: len(x))\n",
    "        return \" \".join(words).capitalize()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower() # 首字母转小写\n",
    "        text = text.split(\" \") # 通过 空格 分割成数组\n",
    "        text.sort(key=lambda x: len(x)) # 使用 lambda 按照元素长度排序\n",
    "        return ' '.join(text).capitalize() # 最后首字母大写\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        words = text.split()\n",
    "        words.sort(key=lambda x:len(x))\n",
    "        return ' '.join(words).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower()\n",
    "        text = text.split(\" \")\n",
    "        text.sort(key = lambda x: len(x))\n",
    "        \n",
    "        return \" \".join(text).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower() # 首字母转小写\n",
    "        text = text.split(\" \") # 通过 空格 分割成数组\n",
    "        text.sort(key=lambda x: len(x)) # 使用 lambda 按照元素长度排序\n",
    "        return ' '.join(text).capitalize() # 最后首字母大写\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        temp = sorted(text.split(\" \"),key = lambda x:len(x))\n",
    "        return ' '.join(temp).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower() # 首字母转小写\n",
    "        text = text.split(\" \") # 通过 空格 分割成数组\n",
    "        text.sort(key=lambda x: len(x)) # 使用 lambda 按照元素长度排序\n",
    "        return ' '.join(text).capitalize() # 最后首字母大写\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        words = text.split()\n",
    "        words.sort(key = len)       \n",
    "        return ' '.join(words).capitalize()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        '''\n",
    "        s,d = [],[]\n",
    "        lt = list(text)\n",
    "        for i in lt:\n",
    "            if i != ' ':\n",
    "                d.append(i)\n",
    "                continue\n",
    "            else:\n",
    "                temp = ''.join(d)\n",
    "                s.append(temp)\n",
    "                d.clear()\n",
    "                continue\n",
    "        temp = ''.join(d)\n",
    "        s.append(temp)\n",
    "        s.sort(key = len)\n",
    "        ret = ''\n",
    "        for i in range(len(s)):\n",
    "            ret += ''.join(s[i])\n",
    "            if i < len(s)-1:\n",
    "                ret += ' '\n",
    "        ret = ret.capitalize()\n",
    "        return ret\n",
    "        '''\n",
    "        ret = text.split()\n",
    "        ret.sort(key=len)\n",
    "        return ' '.join(ret).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower() # 首字母转小写\n",
    "        text = text.split(\" \") # 通过 空格 分割成数组\n",
    "        text.sort(key=lambda x: len(x)) # 使用 lambda 按照元素长度排序\n",
    "        return ' '.join(text).capitalize() # 最后首字母大写\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower() # 首字母转小写\n",
    "        text = text.split(\" \") # 通过 空格 分割成数组\n",
    "        text.sort(key=lambda x: len(x)) # 使用 lambda 按照元素长度排序\n",
    "        return ' '.join(text).capitalize() # 最后首字母大写\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        return ' '.join(sorted(text.lower().split(' '),key=len)).capitalize()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text=chr(ord(text[0])-(ord('A')-ord('a')))+text[1:]\n",
    "        words=list(text.split())\n",
    "        res=[]\n",
    "        slen=len(words)\n",
    "        maxlen=0\n",
    "        for i in words:maxlen=max(maxlen,len(i))\n",
    "        kong,hashmap=[],[]\n",
    "        for i in range(maxlen+1):hashmap.append(kong[:])\n",
    "        for i in words:\n",
    "            hashmap[len(i)].append(i)\n",
    "        print(hashmap)\n",
    "        result=''\n",
    "        for i in hashmap:\n",
    "            for j in i:\n",
    "                result+=j+' '\n",
    "        result=chr(ord(result[0])-ord('a')+ord('A'))+result[1:-1]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower()\n",
    "        text_list = text.split(\" \")\n",
    "        text_list.sort(key = lambda x:len(x))\n",
    "        return ' '.join(text_list).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower()\n",
    "        text = text.split(\" \")\n",
    "        text.sort(key = lambda x: len(x))\n",
    "        return ' '.join(text).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text = text.lower()\n",
    "        words = text.split()\n",
    "        words.sort(key=lambda x: len(x))\n",
    "        return ' '.join(words).capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        h = collections.defaultdict(list)\n",
    "        for w in text.split(' '):\n",
    "            h[len(w)].append(w.lower())\n",
    "        sortedArr = sorted(list(h.keys()))\n",
    "        ansList = []\n",
    "        for v in sortedArr:\n",
    "            for newWord in h[v]:\n",
    "                ansList.append(newWord)\n",
    "        ansList[0] = ansList[0][0].upper() + ansList[0][1:]\n",
    "        return ' '.join(ansList)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        slist = text.split()\n",
    "        slist[0] = slist[0].lower()\n",
    "        print(slist)\n",
    "        l1 = sorted([(s, len(s)) for s in slist], key=lambda x: x[1])\n",
    "        l1[0] = (l1[0][0].capitalize(), l1[0][1])\n",
    "\n",
    "        return ' '.join([l[0] for l in l1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        texts = text.split(\" \")\n",
    "        print(texts)\n",
    "        texts.sort(key = lambda x: len(x))\n",
    "        texts_ = []\n",
    "        for i, t in enumerate(texts):\n",
    "            if i == 0:\n",
    "                t = t.title()\n",
    "            else:\n",
    "                t = t.lower()\n",
    "            texts_.append(t)\n",
    "        return \" \".join(texts_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text_list = text.split(' ')\n",
    "        text_list = [(i, len(i)) for i in text_list]\n",
    "        text_list.sort(key = lambda x:x[-1])\n",
    "        # text_list = text_list[::-1]\n",
    "        text_list = [i[0] for i in text_list]\n",
    "        text = ' '.join(text_list)\n",
    "        text = text.lower()\n",
    "        # k = text[0].upper()\n",
    "        return(text[0].upper() + text[1:])\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        text_list = text.split(' ')\n",
    "        text_list = [(i, len(i)) for i in text_list]\n",
    "        text_list.sort(key = lambda x:x[-1])\n",
    "        # text_list = text_list[::-1]\n",
    "        text_list = [i[0] for i in text_list]\n",
    "        text = ' '.join(text_list)\n",
    "        text = text.lower()\n",
    "        k = text[0].upper()\n",
    "        return(k + text[1:])\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        s,d = [],[]\n",
    "        lt = list(text)\n",
    "        for i in lt:\n",
    "            if i != ' ':\n",
    "                d.append(i)\n",
    "                continue\n",
    "            else:\n",
    "                temp = ''.join(d)\n",
    "                s.append(temp)\n",
    "                d.clear()\n",
    "                continue\n",
    "        temp = ''.join(d)\n",
    "        s.append(temp)\n",
    "        s.sort(key = len)\n",
    "        ret = ''\n",
    "        for i in range(len(s)):\n",
    "            ret += ''.join(s[i])\n",
    "            if i < len(s)-1:\n",
    "                ret += ' '\n",
    "        ret = ret.capitalize()\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        cnt = [(i,len(i)) for i in text.split(' ')]\n",
    "        def help(l1,l2):\n",
    "            l = []\n",
    "            i,j = 0,0\n",
    "            while i < len(l1) and j < len(l2):\n",
    "                if l1[i][1] <= l2[j][1]:\n",
    "                    l.append(l1[i])\n",
    "                    i += 1\n",
    "                else:\n",
    "                    l.append(l2[j])\n",
    "                    j += 1\n",
    "            while i<len(l1):\n",
    "                l.append(l1[i])\n",
    "                i += 1\n",
    "            while j < len(l2):\n",
    "                l.append(l2[j])\n",
    "                j += 1\n",
    "            return l\n",
    "        def merge(l):\n",
    "            if len(l) <= 1: return l\n",
    "            m = len(l)>>1\n",
    "            l1 = merge(l[:m])\n",
    "            l2 = merge(l[m:])\n",
    "            return help(l1,l2)\n",
    "        ans = merge(cnt)\n",
    "        ans = [i[0].lower() for i in ans]\n",
    "        ans[0] = ans[0].capitalize()\n",
    "        \n",
    "        return ' '.join(ans)            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        cnt = [(i,len(i)) for i in text.split(' ')]\n",
    "        print(cnt)\n",
    "        def help(l1,l2):\n",
    "            l = []\n",
    "            i,j = 0,0\n",
    "            while i < len(l1) and j < len(l2):\n",
    "                if l1[i][1] <= l2[j][1]:\n",
    "                    l.append(l1[i])\n",
    "                    i += 1\n",
    "                else:\n",
    "                    l.append(l2[j])\n",
    "                    j += 1\n",
    "            while i<len(l1):\n",
    "                l.append(l1[i])\n",
    "                i += 1\n",
    "            while j < len(l2):\n",
    "                l.append(l2[j])\n",
    "                j += 1\n",
    "            return l\n",
    "        def merge(l):\n",
    "            if len(l) <= 1: return l\n",
    "            m = len(l)>>1\n",
    "            l1 = merge(l[:m])\n",
    "            l2 = merge(l[m:])\n",
    "            return help(l1,l2)\n",
    "        ans = merge(cnt)\n",
    "        ans = [i[0].lower() for i in ans]\n",
    "        ans[0] = ans[0].capitalize()\n",
    "        \n",
    "        return ' '.join(ans)            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        texts = [[word, i] for i,word in enumerate(text.split(' '))]\n",
    "        texts[0][0] = str.lower(texts[0][0][0]) + texts[0][0][1:]\n",
    "        texts.sort(key=lambda x:(len(x[0]),x[1]))\n",
    "        texts[0][0] = str.upper(texts[0][0][0]) + texts[0][0][1:]\n",
    "        return ' '.join([word for word,_ in texts])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrangeWords(self, text: str) -> str:\n",
    "        \n",
    "        t = text.lower().split()\n",
    "        s = [(w, len(w), i) for i, w in enumerate(t)]\n",
    "        s.sort(key=lambda x:(x[1], x[2]))\n",
    "        ans = ' '.join([x for x, _, i in s])\n",
    "        return ans.capitalize()"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
